<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends TMT_Controller {

	public function __construct()
	{
		parent::__construct();

		//Load dependencies
		$this->load->helper(array('form'));

		//	Tank Auth
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');

		//Anonymous Login so no layout
		$this->set_layout_view(FALSE);
	}


	public function index()
	{
		redirect('/auth/login/');
	}

	public function login()
	{
		$error = '';

		if($this->uri->segment(3) !== FALSE)
		{
			$error = 'Fehler beim Login.';
		}

		$this->view_data['form']['target'] = 'auth/check_login';
		$this->view_data['form']['email'] = array(
				'name' => 'email',
				'type' => 'email',
				'autofocus' => 'autofocus',
				'placeholder' => '@t-systems.com',
				'required' => 'required');

		$this->view_data['form']['password'] = array(
				'name' => 'password',
				'type' => 'password',
				'placeholder' => 'Passwort',
				'required' => 'required'
		);
			
		$this->view_data['form']['submit'] = array(
				'name' => 'anmelden',
				'class' => 'btn',
				'type' => 'submit',
				'value' => 'Anmelden');
			
		$this->view_data['error'] = $error;
	}

	public function check_login()
	{

		$this->load->library('form_validation');

		$redirect_path = 'auth/login/error';

		$this->form_validation->set_rules('email','E-Mail','trim|required|valid_email|xss-clean');
		$this->form_validation->set_rules('password','Passwort','trim|required|xss-clean');
		$this->form_validation->set_rules('anmelden','Anmelden','required');

		if($this->form_validation->run())
		{
			if ($this->tank_auth->login(
					$this->form_validation->set_value('email'),
					$this->form_validation->set_value('password'),
					FALSE,											//remember
					FALSE,											//Login by Username
					TRUE)) 											//Login by E-Mail
			{
				// success
				$redirect_path = '';

			}
		}

		redirect($redirect_path);
	}

	function register()
	{
		$this->load->library('form_validation');
		$this->set_content_view('auth/register_form');
		$this->headline = 'Registration!';
		$this->set_layout_view('template');

		$teamleader = Model\Roletype::find_by_name('Teamleitung', FALSE)->employee();
		$this->view_data['emails'] = array();
		foreach($teamleader as $teamleader_item)
		{
			$this->view_data['emails'][$teamleader_item->contactdata()->email] = $teamleader_item->contactdata()->email;
		}
		if (!$this->config->item('allow_registration', 'tank_auth')) {	// registration is off
			$this->_show_message($this->lang->line('auth_message_registration_disabled'));

		} else {
			$use_username = $this->config->item('use_username', 'tank_auth');
			if ($use_username) {
				$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_length', 'tank_auth').']|alpha_dash');
			}
			$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
			$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
			$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');

			$captcha_registration	= $this->config->item('captcha_registration', 'tank_auth');
			$use_recaptcha			= $this->config->item('use_recaptcha', 'tank_auth');
			if ($captcha_registration) {
				if ($use_recaptcha) {
					$this->form_validation->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback__check_recaptcha');
				} else {
					$this->form_validation->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required|callback__check_captcha');
				}
			}
			$data['errors'] = array();

			$email_activation = $this->config->item('email_activation', 'tank_auth');

			if ($this->form_validation->run()) {								// validation ok
				if (!is_null($data = $this->tank_auth->create_user(
						$use_username ? $this->form_validation->set_value('username') : '',
						$this->form_validation->set_value('email'),
						$this->form_validation->set_value('password'),
						$email_activation))) {									// success

					$data['site_name'] = $this->config->item('website_name', 'tank_auth');

					if ($email_activation) {									// send "activate" email
						$data['activation_period'] = $this->config->item('email_activation_expire', 'tank_auth') / 3600;

						$this->_send_email('activate', $data['email'], $data);

						unset($data['password']); // Clear password (just for any case)

						$this->_show_message($this->lang->line('auth_message_registration_completed_1'));

					} else {
						if ($this->config->item('email_account_details', 'tank_auth')) {	// send "welcome" email

							$this->_send_email('welcome', $data['email'], $data);
						}
						unset($data['password']); // Clear password (just for any case)

						$this->_show_message($this->lang->line('auth_message_registration_completed_2').' '.anchor('/auth/login/', 'Login'));
					}
				} else {
					$errors = $this->tank_auth->get_error_message();
					foreach ($errors as $k => $v)	$data['errors'][$k] = $this->lang->line($v);
				}
			}
			if ($captcha_registration) {
				if ($use_recaptcha) {
					$data['recaptcha_html'] = $this->_create_recaptcha();
				} else {
					$data['captcha_html'] = $this->_create_captcha();
				}
			}
			$data['use_username'] = $use_username;
			$data['captcha_registration'] = $captcha_registration;
			$data['use_recaptcha'] = $use_recaptcha;
			$this->load->view('auth/register_form', $data);
		}
	}


	public function logout()
	{
		$this->tank_auth->logout();
		redirect('');
	}
}